﻿@using OPM.Class;
@using System.Web.Mvc;

@{
    var case_id = string.IsNullOrWhiteSpace(Request["case_id"]) ? Request["id"] : Request["case_id"];
    Layout = "~/Views/Shared/_LayoutPopup.cshtml";
    var contactObj = WSO.GetCase(U.Token, case_id);
    if (contactObj == null)
    {
        @:Error: Cannot Load Case
        return;
    }

    var pageId = Request["page_id"] == "2" ?  "เจ้าหน้าที่":"หน่วยงาน";
    var terminalOrgDic = new Dictionary<string, string>(); // org_id, org_text
    terminalOrgDic.Add("-1", "..กรุณาเลือก..");
    foreach (var lcon in contactObj.list_case_org_owner ?? new List<CaseOrgOwner>())
    {
        terminalOrgDic.Add(lcon.org_id, lcon.org_text);
    }
    var terminalSelectListWithoutOption1s = new SelectList(terminalOrgDic.Skip(1), "key", "value");
    var terminalSelectLists = new SelectList(terminalOrgDic, "key", "value", Request["org_id"]);
     
    //initial officer
    OfficerProfile[] officers = null;
    int pageRecordStart = 0, pageRecordEnd = 0, pageNo = 0, amountPage = 0, prevPageNo = 0, nextPageNo = 0;
    bool pleaseLoadResultOfficer = false;
    if (Request["page_id"] == "2")
    {
        pleaseLoadResultOfficer = Request["org_id"] != null && Request["org_id"] != "-1";
        officers = pleaseLoadResultOfficer ? WSO.GetCaseOfficer(U.Token, case_id, Request["org_id"], 0, 1000) : new OfficerProfile[0];
        
        Util.GetPagingData(Request["page_no"], officers.Count(), out pageRecordStart, out pageRecordEnd, out amountPage, out pageNo, out prevPageNo, out nextPageNo);
    }
} 

<div class="service_search_header">
    <div class="service_search_title floatL">@Multi.rec_des</div>
    <div class="clear"></div>
</div>
<div class="service_search_header2">
    <div class="service_title">
        <a href="@Url.Action("SelectTerminal", new { a = Request["a"], case_id = case_id, page_id = 1 })" class="@(pageId=="หน่วยงาน"?"active_tab":"")">
            @Multi.Organization 
        </a>
        |
        <a href="@Url.Action("SelectTerminal", new { a = Request["a"], case_id = case_id, page_id = 2 })" class="@(pageId=="เจ้าหน้าที่"?"active_tab":"")">
            @Multi.offical
        </a>
    </div>
</div>

@if (pageId != "เจ้าหน้าที่")
{
    <div class="service_search_box_table dNone">
        <table id="service_search_table">
            @foreach (var list in terminalSelectListWithoutOption1s)
            {
                <tr>
                    <td onclick="changeToPopup1('@list.Value', '@list.Text', null,null);">@list.Text</td>
                </tr>
            } 
        </table>
    </div>
}
else
{ 
    <div class="service_box_table2">
        <div class="showTextFormBox">
            <div class="floatL">@Multi.Organization</div>
            <div class="floatL">:</div>
            <div class="showTextForm">
                 @Html.DropDownList("ChooseOfficerOrgFilter", terminalSelectLists)
                @if (!pleaseLoadResultOfficer)
                {
                    @:<div style="color:red; width:100%">* กรุณาเลือกหน่วยงาน แล้วกดปุ่มค้นหา</div>
                }
            <div class="clear"></div>
        </div>
        <div class="showTextFormBox">
            <div class="floatL">@Multi.recipient</div>
            <div class="floatL">:</div>
            <div class="showTextForm">
                <input id="name-input" type="text" name="name-input" value="">
            </div>
            </div>
            <div class="clear"></div>
        </div>
        <div class="showTextFormBox">
            <div class="showTextForm">
                <a onclick="find()"><span class="dBlock findBtn" style="cursor:pointer">@Multi.search</span></a>
                <script>
                    function find() {

                        var url = '@Html.Raw(Url.Action("SelectTerminal", new { a = Request["a"], case_id = case_id, page_id = 2, page_no = "F" }))&org_id=' + $('#ChooseOfficerOrgFilter').val();
                        //alert(url);
                        window.location = url;
                    }
                </script>
            </div>
        </div>
        <table class="boxPersondetail">
            <thead>
                <tr>
                    <th>@Multi.row</th>
                    <th>user</th>
                    <th>@Multi.name</th>
                    <th>@Multi.Organization</th>
                    <th>&nbsp;</th>
                </tr>
            </thead>
            <tbody>
@foreach (var officer in officers)
{
    <tr>
        <td>@(pageRecordStart++)</td>
        <td>@officer.username</td>
        <td>@officer.name</td>
        <td>@officer.org_text</td>
        <td>
            <div class="chooseBtn" onclick="changeToPopup1(null,null,'@(officer.id)', '@(officer.name)', null,null);">@Multi.select</div>
        </td>
    </tr> 
}
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="2">@Multi.row_num @pageRecordStart-@pageRecordEnd</td>
                    <td colspan="3">
                        <div class="floatR txtArrow">@Multi.from_all @amountPage @Multi.page</div>
                        <a href="@Url.Action("SelectTerminal", new { a = Request["a"], case_id = case_id, page_id = 2, page_no = "F" })"><span class="floatR dBlock BtnArrow5"></span></a>
                        <a href="@Url.Action("SelectTerminal", new { a = Request["a"], case_id = case_id, page_id = 2, page_no = prevPageNo })"><span class="floatR dBlock BtnArrow4"></span></a>
                        <span class="floatR dBlock BtnArrow3">@pageNo</span>
                        <a href="@Url.Action("SelectTerminal", new { a = Request["a"], case_id = case_id, page_id = 2, page_no = nextPageNo })"><span class="floatR dBlock BtnArrow2"></span></a>
                        <a href="@Url.Action("SelectTerminal", new { a = Request["a"], case_id = case_id, page_id = 2, page_no = "L" })"><span class="floatR dBlock BtnArrow1"></span></a>
                        <div class="floatR txtArrow">หน้า</div>
                        <div class="clear"></div>
                    </td>
                </tr>
            </tfoot>
        </table>

    </div>
    <div class="BoxServiceBottom">
        <div class="BoxServiceActionPrev floatL">@Multi.close</div>
        <div class="BoxServiceActionNext floatR">@Multi.save_as</div>
        <div class="clear"></div>
    </div>
}

<style>
    .active_tab {
        color: green !important;
        font-weight: bold;
    }
</style>

<script>

    function changeToPopup1(terminal_org_id,terminal_org_text,terminal_officer_id,terminal_officer_text) {
        parent.formJson.terminal_org_id = terminal_org_id;
        parent.formJson.terminal_officer_id = terminal_officer_id;
        parent.formJson.terminal_text = terminal_org_text ? terminal_org_text : terminal_officer_text; 
        parent.$.colorbox.close();
    }
    </script>